//
//  SaveViewControllerForVideo.m
//  OpenEyes
//
//  Created by dlios on 15/9/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SaveViewControllerForVideo.h"
#import "MoviewDailyCell.h"
#import "VideoVC.h"
#import "MovieDetaiVC.h"
#import "CoreDataManager.h"
@interface SaveViewControllerForVideo ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *saveTableView;
@property(nonatomic, strong)NSMutableArray *saveArr;
@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation SaveViewControllerForVideo

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.manager = [CoreDataManager shareCoreDataManager];
    [self createSaveControllerTableView];
    [self createSaveTopView];
}
- (void)viewWillAppear:(BOOL)animated
{
    self.saveArr = [self.manager getAllDataWithEntityStyle:Save];
    [self.saveTableView reloadData];
}
- (void)createSaveTopView
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    [self.view addSubview:aView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 25)];
    titleLabel.center = CGPointMake(WIDTH / 2, 64 - 25);
    titleLabel.text = @"我的收藏";
    [aView addSubview:titleLabel];
    
    UIButton *leftBu = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBu.frame = CGRectMake(15, 32, 15, 15);
    [leftBu setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [leftBu addTarget:self action:@selector(leftBuAction) forControlEvents:UIControlEventTouchUpInside];
    [aView addSubview:leftBu];
    
}

- (void)leftBuAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)createSaveControllerTableView
{
    self.saveTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 60) style:UITableViewStylePlain];
    [self.view addSubview:self.saveTableView];
    self.saveTableView.separatorStyle = 0;
    self.saveTableView.delegate = self;
    self.saveTableView.dataSource = self;
    self.saveTableView.rowHeight = 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.saveArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *saveReuse = @"saveReuse";
    MoviewDailyCell *cell = [tableView dequeueReusableCellWithIdentifier:saveReuse];
    if (!cell) {
        cell = [[MoviewDailyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:saveReuse];
    }
    VideoModel *model = self.saveArr[indexPath.row];
    
    NSString *name = @"gif.gif";
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *image = [UIImage sd_animatedGIFWithData:imageData];
  
    
    [cell.backImageView sd_setImageWithURL:[NSURL URLWithString:model.coverForFeed]  placeholderImage:image];
    cell.titleLabel.text = model.title;
    cell.detailLabel.text = [NSString stringWithFormat:@"%@ / %@", model.category, model.duration];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDetaiVC *vc = [[MovieDetaiVC alloc] init];
    vc.dailyListArr = [NSMutableArray array];
    [vc.dailyListArr addObject:[self.manager getAllDataWithEntityStyle:Save]];
    vc.movieIndexPath = indexPath;
    [vc setModalTransitionStyle:2];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        VideoModel *model = self.saveArr[indexPath.row];
        SaveDaily *daily = [[self.manager selectData:model.title EntityStyle:Save] firstObject];
        [self.manager.managedObjectContext deleteObject:daily];
        [self.manager saveContext];
        [self.saveArr removeObjectAtIndex:indexPath.row];
        [self.saveTableView reloadData];
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"取消收藏";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
